<?php

namespace App\Admin\Controllers;

use App\BrokerCall;
use App\BrokerMedia;
use App\Http\Controllers\Controller;
use App\Repository\BrokerRepository;
use App\Service\TeleService;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class BrokerCallController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('通话记录')
            ->description('通话记录描述')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('Create')
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new BrokerCall);
        $grid->model()->orderBy('id','desc');
        $grid->id('Id');
      //  $grid->seqId('SeqId');
        $grid->virtualMobile('绑定账号');
        $grid->from('拨打电话');
        $grid->to('接听电话');
        $grid->callStart('开始时间');
        $grid->callAnswer('接听时间');
        $grid->callEnd('结束时间');
      //  $grid->mid('Mid');
        $grid->call('被叫号码')->display(function($value){
            return $value==1 ? '呼叫' : '被叫';
        });
        $grid->status('状态')->display(function($value){
            switch ($value){
                case -1:
                    return '实时来电通知';
                    break;
                case 0:
                    return '正常接听';
                    break;
                case 1:
                    return '未应答';
                    break;
                case 2:
                    return '黑名单来电，拒接';
                    break;
                case 3:
                    return '虚号关机状态，未接听';
                    break;
                case 4:
                    return '未绑定号码';
                    break;
            }
        });
        $grid->noAnswerReason('未接听原因');
       // $grid->created_at('Created at');

        $grid->video('录音')->display(function($value){

            if(!$value){

              $brokerMedia=  BrokerMedia::where("mid",$this->mid)->first();

              if($brokerMedia){
                 $brokerRes= new BrokerRepository();
                 $value=\Storage::url($brokerRes->getVideo($brokerMedia));
              }
            }else{
                $value=\Storage::url($value);
            }
return   "<audio controls=\"controls\">
<source src=\"$value\" type=\"audio/mpeg\">
</audio>";
        });
        $grid->disableCreateButton();
        $grid->disableActions();

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(BrokerCall::findOrFail($id));

        $show->seqId('SeqId');
        $show->virtualMobile('VirtualMobile');
        $show->id('Id');
        $show->from('From');
        $show->to('To');
        $show->callStart('CallStart');
        $show->callAnswer('CallAnswer');
        $show->callEnd('CallEnd');
        $show->mid('Mid');
        $show->call('Call');
        $show->status('Status');
        $show->noAnswerReason('NoAnswerReason');
        $show->created_at('Created at');
        $show->updated_at('Updated at');
        $show->video('Video');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new BrokerCall);

        $form->text('seqId', 'SeqId');
        $form->text('virtualMobile', 'VirtualMobile');
        $form->text('from', 'From');
        $form->text('to', 'To');
        $form->datetime('callStart', 'CallStart')->default(date('Y-m-d H:i:s'));
        $form->datetime('callAnswer', 'CallAnswer')->default(date('Y-m-d H:i:s'));
        $form->datetime('callEnd', 'CallEnd')->default(date('Y-m-d H:i:s'));
        $form->datetime('mid', 'Mid')->default(date('Y-m-d H:i:s'));
        $form->number('call', 'Call');
        $form->number('status', 'Status');
        $form->text('noAnswerReason', 'NoAnswerReason');
        $form->text('video', 'Video');
        return $form;
    }
}
